<?php

namespace app\admin\controller\topics;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="topics")
 */
class Topics extends AdminController
{

    use \app\admin\traits\Curd;
    protected $relationSerach = true;
    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Topics();
        
        $this->assign('getTypeList', $this->model->getTypeList());

    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFieds')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->withJoin(['course','subjects'],'', 'LEFT')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin(['course','subjects'], 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }


    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            //组装题目
            $data =[
                'cid' => $post['cid'],
                'title' => $post['title'],
                'type' => $post['type'],
                'images' => $post['images'],
                'answers' => $post['answers'],
                'analysis' => $post['analysis']
            ];
            $i = 0;
            foreach ($post as $k => $val){
                $options = explode('-',$k);
                if($options[0] == "options" && $val){
                    $data['options'][$i] = [$options[1]=>$val];
                    $i++;
                }
            }
            if(!$data['options']){
                $this->error('选项不能为空');
            }
            $data['options'] = json_encode($data['options']);
            //获取科目id
            $data['sid'] = Db::name('course')->alias('a')->join('ea_subjects b','a.sid = b.id','left')->where('a.id',$post['cid'])->value('b.id');
            try {
                $save = $this->model->save($data);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();

            //组装题目
            $data =[
                'cid' => $post['cid'],
                'title' => $post['title'],
                'type' => $post['type'],
                'images' => $post['images'],
                'answers' => $post['answers'],
                'analysis' => $post['analysis']
            ];
            $i = 0;
            foreach ($post as $k => $val){
                $options = explode('-',$k);
                if($options[0] == "options" && $val){
                    $data['options'][$i] = [$options[1]=>$val];
                    $i++;
                }
            }
            if(!$data['options']){
                $this->error('选项不能为空');
            }
            $data['options'] = json_encode($data['options']);
            //获取科目id
            $data['sid'] = Db::name('course')->alias('a')->join('ea_subjects b','a.sid = b.id','left')->where('a.id',$post['cid'])->value('b.id');

            try {
                $save = $row->save($data);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    
}